package icegps.com.pingdj.map.utils;


import icegps.com.pingdj.App;
import icegps.com.pingdj.constant.MapCons;
import icegps.com.pingdj.map.bean.MapMeasureInfo;
import icegps.com.pingdj.map.bean.XYCAD;
import icegps.com.pingdj.map.listener.OnInfoAndConfigCallback;

import static icegps.com.pingdj.constant.Constant.measureAveHeight;

/**
 * Created by 111 on 2018/2/28.
 */

public class MeasureInfoManager {
    private double maxHeight;//最大高度
    private double minHeight;//最低高度
    private double aveHeight;//平均高度
    private double sumHeight;//总高度
    private double currentAltitude;//当前高度
    private double soilVolume;//土方量
    private int count;//测量总数
    private double area;//面积
    private MapMeasureInfo mapMeasureInfoNew;
    private MapMeasureInfo mapMeasureInfoOld;
    private long time;
    private OnInfoAndConfigCallback onHeigthInfoCallback;
    private TileUtils tileUtils;

    public MeasureInfoManager(long loadToken, long saveToken, TileUtils tileUtils) {
        this.tileUtils = tileUtils;
        mapMeasureInfoOld = App.getInstance().getDaoSession().getMapMeasureInfoDao().load(loadToken);
        mapMeasureInfoNew = new MapMeasureInfo();
        if (mapMeasureInfoOld != null) {
            maxHeight = mapMeasureInfoOld.getMax();
            minHeight = mapMeasureInfoOld.getMin();
            aveHeight = mapMeasureInfoOld.getAve();
            sumHeight = mapMeasureInfoOld.getSum();
            count = mapMeasureInfoOld.getCount();
            area = mapMeasureInfoOld.getArea();
            soilVolume = mapMeasureInfoOld.getSoilVolume();
            currentAltitude = mapMeasureInfoOld.getCurrentAltitude();
            mapMeasureInfoNew.setMax(mapMeasureInfoOld.getMax());
            mapMeasureInfoNew.setMin(mapMeasureInfoOld.getMin());
            mapMeasureInfoNew.setAve(mapMeasureInfoOld.getAve());
            mapMeasureInfoNew.setSum(mapMeasureInfoOld.getSum());
            mapMeasureInfoNew.setCount(mapMeasureInfoOld.getCount());
            mapMeasureInfoNew.setArea(mapMeasureInfoOld.getArea());
            mapMeasureInfoNew.setSoilVolume(mapMeasureInfoOld.getSoilVolume());
            mapMeasureInfoNew.setCurrentAltitude(mapMeasureInfoOld.getCurrentAltitude());

        }
        mapMeasureInfoNew.setId(saveToken);
    }

    public void startSaveSQData(final XYCAD xycad) {
        double height = xycad.getAltitude();
        count++;
        sumHeight += height;
        if (maxHeight == 0) {
            maxHeight = (float) height;
        }
        if (minHeight == 0) {
            minHeight = (float) height;
        }
        if (height > maxHeight) {
            maxHeight = (float) height;
        }
        if (height < minHeight) {
            minHeight = (float) height;
        }
        currentAltitude = xycad.getAltitude();
        aveHeight = (float) (sumHeight / count);

        Float difference = tileUtils.locationExists(xycad.getX(), xycad.getY());

        if (difference == null) {
            area += MapCons.mapAccuracy * MapCons.mapAccuracy;
        } else {
            soilVolume -= MapCons.mapAccuracy * MapCons.mapAccuracy * difference;
        }
        soilVolume += MapCons.mapAccuracy * MapCons.mapAccuracy * xycad.getDifference();
        soilVolume = Math.abs(soilVolume);

        measureAveHeight = aveHeight;
        mapMeasureInfoNew.setMax(maxHeight);
        mapMeasureInfoNew.setMin(minHeight);
        mapMeasureInfoNew.setAve(aveHeight);
        mapMeasureInfoNew.setSum(sumHeight);
        mapMeasureInfoNew.setCount(count);
        mapMeasureInfoNew.setArea(area);
        mapMeasureInfoNew.setSoilVolume(soilVolume);
        mapMeasureInfoNew.setCurrentAltitude(currentAltitude);
        mapMeasureInfoNew.setLatitude(xycad.getStartLatitude());
        mapMeasureInfoNew.setLongitude(xycad.getStartLongitude());
        mapMeasureInfoNew.setAltitude(xycad.getAltitude());
        callbackHeightInfo();
    }

    private void callbackHeightInfo() {
        long l = System.currentTimeMillis();
        if (l - time > 1000) {
            if (onHeigthInfoCallback != null) {
                onHeigthInfoCallback.onInfoAndConfig(mapMeasureInfoNew);
            }
            time = l;
        }
    }

    public void saveSQData() {
        save();
    }

    public void setOnHeigthInfoCallback(OnInfoAndConfigCallback onHeigthInfoCallback) {
        this.onHeigthInfoCallback = onHeigthInfoCallback;
        callbackHeightInfo();
    }


    private void save() {
        App.getInstance().getDaoSession().getMapMeasureInfoDao().insertOrReplace(mapMeasureInfoNew);
    }
}
